package com.jike.noobmoney.mvp.view.activity;


import android.content.Intent;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jike.noobmoney.R;
import com.jike.noobmoney.contants.ConstantValue;
import com.jike.noobmoney.entity.MoneyEntity;
import com.jike.noobmoney.mvp.presenter.UserPresenter;
import com.jike.noobmoney.net.IView;
import com.jike.noobmoney.util.ComUtils;
import com.jike.noobmoney.util.SPUtils;
import com.jike.noobmoney.util.ToastUtils;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by kekex on 2019/3/9.
 * 签到  1.获取余额 2.签到
 */

public class QianDaoCopyActivity extends BaseActivity implements IView {

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_money)
    TextView tvMoney;

    @BindView(R.id.tv_check)
    TextView tvCheck;
    @BindView(R.id.ll_check_in)
    LinearLayout llCheckIn;
    @BindView(R.id.tv_check_in)
    TextView tvCheckIn;
    @BindView(R.id.iv_check)
    ImageView ivCheck;
    //@BindView(R.id.tv_daojishi)
    //TextView tvDaojishi;

    @BindView(R.id.tv_yaoqing)
    TextView tvYaoQing;
    @BindView(R.id.tv_push)
    TextView tvPush;

    private UserPresenter userPresenter;
    private String userid;

    @Override
    protected int layoutResID() {
        return R.layout.activity_qiandao;
    }

    private long Toal = 60 * 60 * 1000;

    @Override
    protected void initData() {
        tvTitle.setText("每日签到");
        userPresenter = new UserPresenter(this);
        userid = SPUtils.getInstance().getString(ConstantValue.SpType.userid);
        refreshData();

        long now = System.currentTimeMillis();
        long check_in = SPUtils.getInstance().getLong("checkin_time", 0L);

        //需要倒计时
        //long jiangeTime = now - check_in;
        //long needTime = Toal - jiangeTime;

        //if (jiangeTime > Toal) {
        //    //可以签到
        //    changeState(true);
        //} else {
        //    //不可签到
        //    changeState(false);
        //    daojishi(needTime);
        //}

        String lastTime = SPUtils.getInstance().getString(ConstantValue.SpType.day, "2019-03-11");
        String nowTime = ComUtils.getDay();
        if (nowTime.equals(lastTime)) {
            changeState(false);  //同一天 不可签到
        } else {
            changeState(true);   //不同天 可以签到
        }

        tvYaoQing.setSelected(true);
        tvPush.setSelected(true);
    }


    /**
     * 获取余额
     */
    private void refreshData() {
        showProgress();
        userPresenter.moneyView(userid, ConstantValue.RequestKey.getMoneyView);
    }

    /**
     * 签到
     */
    private void checkIn() {
        showProgress();
        userPresenter.checkin(userid, ConstantValue.RequestKey.checkIn);

    }

    @OnClick({R.id.iv_back, R.id.ll_check_in, R.id.tv_check_in, R.id.tv_yaoqing, R.id.tv_push})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.ll_check_in:
                if (llCheckIn.isSelected()) {
                    checkIn();
                }
                break;
            case R.id.tv_check_in:
                if (tvCheck.isSelected()) {
                    checkIn();
                }
                break;
            case R.id.tv_yaoqing:
                recommend();
                break;
            case R.id.tv_push:
                startActivity(new Intent(this, PushTaskActivity.class));
                break;

        }
    }

    /**
     * 点击分享
     */
    public void recommend() {
        String recode = SPUtils.getInstance().getString(ConstantValue.SpType.recode);//签到页
        String taskurl = SPUtils.getInstance().getString(ConstantValue.SpType.taskurl);
        String url = !TextUtils.isEmpty(taskurl) ? taskurl + "?recode=" + recode : ConstantValue.COMMENTURLCOPY + recode;
        UMWeb web = new UMWeb(url);
        web.setTitle("小白赚钱");
        web.setThumb(new UMImage(context, R.mipmap.ic_launcher));
        web.setDescription(getString(R.string.text_yaoqing1));
        ShareAction shareAction = new ShareAction(this);
        shareAction.withMedia(web);
        shareAction.setDisplayList(SHARE_MEDIA.QZONE, SHARE_MEDIA.QQ, SHARE_MEDIA.WEIXIN, SHARE_MEDIA.WEIXIN_CIRCLE);
        shareAction.open();
    }

    @Override
    public void onSuccess(String code, String msg, Object data, String actionType) {
        dismissProgress();
        if (ConstantValue.RequestKey.getMoneyView.equals(actionType)) {
            //获取用户余额
            MoneyEntity.UserBean userBean = (MoneyEntity.UserBean) data;
            tvMoney.setText("" + userBean.getMoney());
        }

        if (ConstantValue.RequestKey.checkIn.equals(actionType)) {
            ToastUtils.showShortToastSafe("" + msg);
            //签到成功刷新余额
            refreshData();

            long checkinTime = System.currentTimeMillis();
            SPUtils.getInstance().put("checkin_time", checkinTime);

            SPUtils.getInstance().put(ConstantValue.SpType.day, ComUtils.getDay());
            changeState(false); //签到成功
            //daojishi(Toal);
        }

    }

    private void daojishi(long needTime) {
        new CountDownTimer(needTime, 1000) {
            @Override
            public void onTick(long l) {
                //tvDaojishi.setVisibility(View.VISIBLE);
                //tvDaojishi.setText(ComUtils.formatLongToTimeStr(l / 1000));
            }

            @Override
            public void onFinish() {
                //tvDaojishi.setVisibility(View.GONE);
                //可以签到
                //changeState(true);
            }
        }.start();
    }

    private void changeState(boolean isAbleQiandao) {
        if (isAbleQiandao) {
            //可以签到
            llCheckIn.setSelected(true);
            ivCheck.setVisibility(View.VISIBLE);
            tvCheck.setText("签到");

            tvCheckIn.setSelected(true);
            tvCheckIn.setText("去签到");
        } else {
            //不可签到
            llCheckIn.setSelected(false);
            ivCheck.setVisibility(View.GONE);
            tvCheck.setText("已签到");

            tvCheckIn.setSelected(false);
            tvCheckIn.setText("已签到");
        }

    }


    @Override
    public void onFailed(String code, String msg) {
        dismissProgress();
        ToastUtils.showShortToastSafe(msg);
        if ("200".equals(code)) {
            SPUtils.getInstance().put(ConstantValue.SpType.day, ComUtils.getDay());
            changeState(false);
        }
    }
}
